/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.CountryModel;
import com.elf.gwt.common.client.model.ZoneModel;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  
 */
public class ZonesComponent extends EditTableComponent
{	
	protected static CountriesComponent countriesComponent = null;
	protected List<ZoneModel> zoneModels;
	protected ZoneModel emptyZoneModel;

	/**
	 * Constructor
	 */
	public ZonesComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		emptyZoneModel = new ZoneModel();
		zoneModels = new ArrayList<ZoneModel>();
		paginate = true;
		tableType = "zones";
		if (countriesComponent == null) countriesComponent = (CountriesComponent)controller.getComponent("CountriesComponent");
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		super.buildMetaModel(jsonObject);
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		zoneModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		ZoneModel model = new ZoneModel(jsonObject);
		zoneModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = zoneModels.size();
		if (edit)
			numberOfTableColumns = 4;
		else 
			numberOfTableColumns = 3;
		extraRows = 2;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Country");
		grid.setText(0, 1, "Name");
		grid.setText(0, 2, "Code");
		if (edit) grid.setText(0, 3, "Mark for Deletion");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, zoneModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
		displayRow(grid, row, emptyZoneModel);
	}

	public void displayRow(Grid grid, int row, ZoneModel model) {
		if (edit) {
			List<CountryModel> countryModels = countriesComponent.getCountryModels();
			
			Hidden idHidden = new Hidden();
			idHidden.setName("zoneId");
			idHidden.setValue(String.valueOf(model.getId()));
			elements.put("zoneId-" + row, idHidden);
			
			int i = 0;
			ListBox countriesListBox = new ListBox();
			countriesListBox.addItem("None", "0");
			if (0 == model.getCountryId())
			{
				countriesListBox.setSelectedIndex(i);
			}
			i++;
			Iterator<CountryModel> iter1 = countryModels.iterator();
			while(iter1.hasNext())
			{
				CountryModel cmodel = iter1.next();
				countriesListBox.addItem(cmodel.getName(), String.valueOf(cmodel.getId()));
				if (cmodel.getId() == model.getCountryId())
				{
					countriesListBox.setSelectedIndex(i);
				}
				i++;
			}
			elements.put("zoneCountryId-" + row, countriesListBox);
			grid.setWidget(row, 0, countriesListBox);

			TextBox nameTextBox = new TextBox();
			nameTextBox.setWidth("160px");
			nameTextBox.setText(model.getName());
			elements.put("zoneName-" + row, nameTextBox);
			grid.setWidget(row, 1, nameTextBox);

			TextBox codeTextBox = new TextBox();
			codeTextBox.setWidth("100px");
			codeTextBox.setText(model.getCode());
			elements.put("code-" + row, codeTextBox);
			grid.setWidget(row, 2, codeTextBox);

			if (model.getId() > 0) {
				CheckBox deleteCheckBox = new CheckBox();
				elements.put("zoneDelete-" + row, deleteCheckBox);
				grid.setWidget(row, 3, deleteCheckBox);
			}
		}
		else {
			grid.setText(row, 0, model.getCountryName());
			grid.setText(row, 1, model.getName());
			grid.setText(row, 2, model.getCode());
		}
	}

	public List<ZoneModel> getZoneModels() {
		return zoneModels;
	}
}
